package com.yjt.fresh.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.yjt.fresh.R;
import com.yjt.fresh.base.BaseSimpleActivity;

/**
 * 扫描页面
 * Created by UPC on 2018/8/14.
 */

public class AtyQRScan extends BaseSimpleActivity {
    private boolean isOpen;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_qr_scan;
    }

    @Override
    protected void initViews() {
        CaptureFragment captureFragment = new CaptureFragment();

        // 为二维码扫描界面设置定制化界面
        CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera);
        final ImageView ivSwitch = (ImageView) findViewById(R.id.ivMyCameraSwitch);
        final TextView tvSwitch = (TextView) findViewById(R.id.tvMyCameraSwitch);
        RelativeLayout rlBack = findViewById(R.id.back);
        ivSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isOpen) {
                    CodeUtils.isLightEnable(true);
                    ivSwitch.setImageResource(R.mipmap.i_sao_ic_03);
                    tvSwitch.setText("轻触关闭");
                } else {
                    CodeUtils.isLightEnable(false);
                    ivSwitch.setImageResource(R.mipmap.i_sao_ic_03s);
                    tvSwitch.setText("轻触打开");
                }
                isOpen = !isOpen;
            }
        });
        rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        captureFragment.setAnalyzeCallback(analyzeCallback);
        getSupportFragmentManager().beginTransaction().replace(R.id.flAppAtyZxingContainer, captureFragment).commit();
    }

    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
            bundle.putString(CodeUtils.RESULT_STRING, result);
            resultIntent.putExtras(bundle);
            setResult(RESULT_OK, resultIntent);
            finish();
        }

        @Override
        public void onAnalyzeFailed() {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
            bundle.putString(CodeUtils.RESULT_STRING, "");
            resultIntent.putExtras(bundle);
            setResult(RESULT_OK, resultIntent);
            finish();
        }
    };

    @Override
    protected void initEvent() {

    }
}
